package com.doctor.windflower_doctor.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.windflower_doctor.R;
import com.doctor.windflower_doctor.actionBeen.DoctorAction;
import com.doctor.windflower_doctor.db.SharedPreferencesDB;
import com.doctor.windflower_doctor.http.callback.HttpRequestCallBack;
import com.doctor.windflower_doctor.http.parser.impl.JsonParser;
import com.doctor.windflower_doctor.http.request.HttpRequest;
import com.doctor.windflower_doctor.http.request.HttpRequestParams;
import com.doctor.windflower_doctor.http.response.HttpResponseInfo;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class PasswordActivity extends BaseActivity {
	private TextView tv_title,tv_save;
	private ImageView iv_backs;
	private EditText et_old,et_new,et_renew;
	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		iv_backs.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		tv_save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if ((!et_new.getText().toString().equals(et_renew.getText().toString()))) {
					Toast.makeText(getApplicationContext(), "两次密码输入不一致", Toast.LENGTH_SHORT).show();
				}
				else if(et_new.getText().toString().trim().length()<6||et_old.getText().toString().trim().length()<6){
					Toast.makeText(getApplicationContext(),"密码长度不能少于6位", Toast.LENGTH_SHORT).show();
				}
			
				else {
					resetPassword();
				}
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		tv_title = (TextView) findViewById(R.id.textView);
		tv_save=(TextView) findViewById(R.id.tv_save);
		tv_save.setVisibility(View.VISIBLE);
		tv_title.setText("修改密码");
		iv_backs = (ImageView) findViewById(R.id.back_btn);
		et_new=(EditText) findViewById(R.id.et_new);
		et_old=(EditText) findViewById(R.id.et_old);
		et_renew=(EditText) findViewById(R.id.et_renew);
	}

	private void resetPassword(){
		HttpMethod method=HttpMethod.POST;
		String url="http://182.92.79.214:3002/api/user/modify_old_pwd";
		HttpRequestCallBack<DoctorAction> requestCallBack=new HttpRequestCallBack<DoctorAction>(new JsonParser(), DoctorAction.class) {

			@Override
			protected void handleResult(
					HttpResponseInfo<DoctorAction> responseInfo) {
				// TODO Auto-generated method stub
				if(responseInfo.success.success){
						Toast.makeText(getApplicationContext(), "密码修改成功", 1).show();
						finish();
					
				}
				else {
					Toast.makeText(getApplicationContext(), "旧密码错误，请重新输入", 1).show();
				}
			}

			@Override
			protected void handleError(HttpException e, String msg) {
				// TODO Auto-generated method stub
				Toast.makeText(PasswordActivity.this, msg, Toast.LENGTH_SHORT).show();
			}
		};
		HttpRequestParams params = new HttpRequestParams(method, url, requestCallBack);
		params.addBodyParameter("account_id", SharedPreferencesDB.getInstance(this).getDocotorId());
		params.addBodyParameter("oldpwd", et_old.getText().toString().trim());
		params.addBodyParameter("newpwd", et_new.getText().toString().trim());
		params.addBodyParameter("appsecret","123123");
		HttpRequest.send(params);
	}
	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.change_password;
	}

}
